/*
 * @Author: LSC
 */
const Mock = require('mockjs')
const dayjs = require('dayjs')
let merchantManagements = require('./managements')
// for (let i = 0; i < count; i++) {
// merchantManagements.push(Mock.mock({
//   id: i,
//   account: 'account' + i,
//   firstName: 'firstName',
//   lastName: 'lastName' + i,
//   name: 'name' + i,
//   email: `user${i}@gmail.com`,
//   role: 'Merchant user',
//   status: false,//false未锁定
//   'operationTime|1': ['10:00:00 9-4-2021', '10:00:00 8-4-2021', '10:00:00 7-4-2021', '10:00:00 6-4-2021', '10:00:00 5-4-2021'],
//   password: '123456'
// }))
// }

module.exports = [
  // username search
  {
    url: '/vue-element-admin/get/merchantManagements',
    type: 'get',
    response: config => {
      let { page = 1, limit = 10, email, account } = config.query
      let searchArr = JSON.parse(JSON.stringify(merchantManagements))

      // 条件搜索
      if (email) {
        email = email.replace(/\+/g, ' ')
        searchArr = searchArr.filter(item => item.email === email)
      }
      if (account) {
        account = account.replace(/\+/g, ' ')
        searchArr = searchArr.filter(item => item.account === account)
      }
      searchArr.sort((a, b) => {
        return dayjs(b.createTime).valueOf() - dayjs(a.createTime).valueOf()
      })

      const pageList = searchArr.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        count: searchArr.length,
        data: pageList
      }
    }
  },
  {
    url: '/vue-element-admin/getMerchantManagementById',
    type: 'get',
    response: config => {
      const { id } = config.query
      let searchArr = JSON.parse(JSON.stringify(merchantManagements))
      searchArr = searchArr.filter(item => item.id == id)[0]
      return {
        code: 20000,
        data: searchArr
      }
    }
  },
  {
    url: '/vue-element-admin/setMerchantManagement',
    type: 'post',
    response: config => {
      const { id, status } = config.body
      // 条件搜索
      merchantManagements = merchantManagements.map(item => {
        if (item.id == id) {
          item.status = !status
          item.operationTime = new Date()
        }
        return item
      })

      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  },
  // transaction list
  {
    url: '/vue-element-admin/add/merchantManagement',
    type: 'get',
    response: config => {
      const oldArr = JSON.parse(JSON.stringify(merchantManagements))
      const { id, account, firstName, lastName, name, email, password, status } = config.query

      if (id === undefined) {
        merchantManagements.push(Mock.mock({
          id: oldArr.length,
          account,
          firstName,
          lastName,
          name,
          email,
          role: 'Merchant user',
          status: false,
          email: `user${oldArr.length}@gmail.com`,
          operationTime: new Date(),
          createTime: new Date(),
          password
        }))
      } else {
        merchantManagements = merchantManagements.map(item => {
          if (item.id == id) {
            item.account = account
            item.firstName = firstName
            item.lastName = lastName
            item.name = name
            item.operationTime = new Date()
            item.password = password
          }
          return item
        })
      }

      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  },
  {
    url: '/vue-element-admin/saveCommodity',
    type: 'post',
    response: config => {
      const { id, status } = config.body
      // 条件搜索
      merchantManagements = merchantManagements.map(item => {
        if (item.id == id) {
          item.status = status
        }
        return item
      })

      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  }
]
